package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import Controller.Client;
import Controller.DBcontroller;
import Entity.Customer;
import Entity.Worker;
import Entity.Worker.Role;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * this is the service class worker log in GUI.the worker will enter his id an password in order to enter the system.
 * @author Or Marx
 *
 */
public class ServiceClassWorkerLogInGui extends JPanel{
	JFrame frame;
	JPanel lastPanel;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public JPanel getLastPanel()
	{
		return (JPanel)lastPanel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}
	private JTextField textField;
	private JPasswordField passwordField;
	/**
	 * is the controller. it will check the id and password.
	 */
	public ServiceClassWorkerLogInGui() {
		setLayout(null);
		
		JLabel lblLogin = new JLabel("LOGIN");
		lblLogin.setFont(new Font("Tahoma", Font.PLAIN, 39));
		lblLogin.setBounds(384, 32, 207, 71);
		add(lblLogin);
		
		JLabel lblId = new JLabel("ID");
		lblId.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblId.setBounds(253, 166, 81, 29);
		add(lblId);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblPassword.setBounds(253, 221, 120, 29);
		add(lblPassword);
		
		textField = new JTextField();
		textField.setBounds(384, 172, 173, 20);
		add(textField);
		textField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(384, 227, 173, 20);
		add(passwordField);
		
		JButton btnLogin = new JButton("Customer Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String id=textField.getText();
				boolean flag=true;
				String password=passwordField.getText();
				Worker workers[]=DBcontroller.getWorkers();
				for (int i=0; i<workers.length; i++)
					if (workers[i].getWorkerID().equals(id) && workers[i].getPassword().equals(password) && workers[i].getR()==Role.ServiceClassWorker)
					{
						FrameSystem.setLoggedIn(id);
						CustomerServiceWorkerGUI next=new CustomerServiceWorkerGUI();
						next.setFrame(frame);
						next.setLastPanel((JPanel) frame.getContentPane());
						frame.setContentPane(next);
						frame.setVisible(true);
						flag=false;
						break;
					}
				if (flag)
				JOptionPane.showMessageDialog(null,"username or password arent correct!","customer login",JOptionPane.ERROR_MESSAGE);

			}
		});
		btnLogin.setBounds(253, 341, 133, 23);
		add(btnLogin);
		
		JButton btnCancel = new JButton("cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setContentPane(lastPanel);
			}
		});
		btnCancel.setBounds(458, 341, 133, 23);
		add(btnCancel);
	}
}
